<?php
class Access_Token
{
	
    public static function getAccessToken($appid, $appsecret, $clear=FALSE)
    {
        if($appid=='wx7bb325431fe17e06')
        {
            $mm_key = 'wechat_access_token_' . $appid . '_' . $appsecret;
           // $access_token = System_Lib_Memcache::get($mm_key);
          
            $access_token = $this->_Memcache->get($mm_key); 
            if(empty($access_token) || $clear)
            {
                $requestUri = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
                $res = self::send($requestUri);
                $access_token = $res['access_token'];
                $expire = $res['expires_in'] - 600;

                //System_Lib_Memcache::set($mm_key, $access_token, $expire);
                  $this->_Memcache->set($mm_key,$access_token, false, $expire) ;
            }

            return $access_token;
        }
        else
        {
            $dids = array(
                // 纪梵希
                // http://woaap.etocrm.com/givenchy_production.php
                'wx6bda831cc8f20ec1' => array(
                    'did' => '33',
                    'code' => '13050520',
                ),
                // 科颜氏
                'wx211400cf31972b7c' => array(
                    'did' => '77',
                    'code' => '95481130',
                ),
                // 相宜本草
                'wxdfc5e5a9feea1c4b' => array(
                    'did' => '35',
                    'code' => '57626768',
                ),
                  'wxcfa79092304d6a60' => array(
                    'did' => '2',
                    'code' => '42453451',
                ),
            );
            if(!empty($dids[$appid]))
            {
                $requestUri = 'http://woaap.etocrm.com/services/api_get_token.php?did=' . $dids[$appid]['did'] . '&code=' . $dids[$appid]['code'];
            
                $res = self::send($requestUri);
                
                $access_token = $res['access_token'];
         
                return $access_token;
            }
            
            $Accountids = array(
                // 惠氏
                'wx7fd6725300a50716' => array(
                    'aid' => '3DB2ACBD-502D-4E5C-BE68-45B6B903FE0E',
                ),
            );
            if(!empty($Accountids[$appid]))
            {
                $time = time();
                $tmpArr = array($Accountids[$appid]['aid'], $time, 'v2', 'we2014');
                sort($tmpArr, SORT_STRING);
                $tmpStr = implode( $tmpArr );
                $requestUri = 'http://wxapp.wemediacn.com/accesstoken/get/?accountid=' . $Accountids[$appid]['aid'] . '&time=' . $time . '&sign=' . md5($tmpStr);
                
                $res = self::send($requestUri);
                $access_token = $res['msg'];

                return $access_token;
            }
        }
    }
    
    
       public static function send($requestUril)
	{
        
        $res = self::https_request($requestUril);
      
        if(empty($res))
        {
            $res = '{}';
        }
       
        $res = json_decode($res , TRUE);
        
        return $res;
	}
        
        //get方式执行页面
       function https_request($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
        curl_close($curl);
        return $data;
    }
}
?>